<template>
  <common-list>
    <common-search slot="search" @search="handleQuery" @reset="resetQuery">
      <el-form
        ref="queryForm"
        :inline="true"
        :model="queryParams"
        v-show="showSearch"
        @submit.native.prevent
        label-width="90px"
      >
        <el-form-item label="应用名称" prop="applicationName">
          <common-input
            :verify="false"
            v-model="queryParams.applicationName"
            @enter="handleQuery"
            clearable
          />
        </el-form-item>
        <el-form-item label="域名" prop="domain">
          <common-input
            :verify="false"
            @enter="handleQuery"
            v-model="queryParams.domain"
            clearable
          />
        </el-form-item>
        <el-form-item label="IP地址" prop="ip">
          <common-input
            :verify="false"
            v-model="queryParams.ip"
            clearable
            @enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="端口" prop="port">
          <common-input
            :verify="false"
            v-model="queryParams.port"
            clearable
            @enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="业务系统" prop="businessSystemId">
          <el-select v-model="queryParams.businessSystemId" filterable>
            <el-option label="全部" :value="null"></el-option>
            <el-option
              v-for="item in businessList"
              :key="item.dictValue"
              :label="item.dictLabel"
              :value="item.dictValue"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="状态" prop="interfaceSwitch">
          <el-select v-model="queryParams.interfaceSwitch" filterable>
            <el-option label="全部" :value="null"></el-option>
            <el-option label="上线" :value="0"></el-option>
            <el-option label="下线" :value="1"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
    </common-search>
    <el-row :gutter="10" slot="tools">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          @click="handleAdd"
          v-hasPermi="['apisecurity:applicationManage:add']"
          >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          plain
          type="primary"
          icon="el-icon-top"
          :disabled="!showBatch"
          @click="changeAllStatus(0)"
          v-hasPermi="['apisecurity:applicationManage:remove']"
          >批量上线
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          plain
          type="primary"
          icon="el-icon-bottom"
          :disabled="!showBatch"
          @click="changeAllStatus(1)"
          v-hasPermi="['apisecurity:applicationManage:remove']"
          >批量下线
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          plain
          type="danger"
          icon="el-icon-delete"
          :disabled="!showBatch"
          @click="handleDelete()"
          v-hasPermi="['apisecurity:applicationManage:remove']"
          >批量删除
        </el-button>
      </el-col>
    </el-row>

    <common-table
      slot="content"
      v-loading="loading"
      :data="list"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column
        label="应用名称"
        align="left"
        prop="applicationName"
        show-overflow-tooltip
        fixed="left"
      />

      <el-table-column
        label="域名"
        align="left"
        prop="domain"
        show-overflow-tooltip
      />

      <el-table-column label="IP地址" prop="ip" show-overflow-tooltip />
      <el-table-column
        label="端口"
        prop="port"
        width="100"
        show-overflow-tooltip
      />
      <el-table-column
        label="业务系统"
        prop="businessSystemName"
        width="200"
        show-overflow-tooltip
      />
      <el-table-column
        label="创建时间"
        prop="createTime"
        width="170"
        show-overflow-tooltip
      />
      <el-table-column label="状态" align="center" width="80px">
        <template slot-scope="scope">
          <el-tag type="success" v-if="scope.row.interfaceSwitch === 0"
            >上线
          </el-tag>
          <el-tag type="danger" v-else-if="scope.row.interfaceSwitch === 1"
            >下线
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="left" width="160" fixed="right">
        <template slot-scope="scope">
          <template>
            <el-button type="text" @click="hanldeDetail(scope.row)"
              >详情
            </el-button>
            <el-button
              type="text"
              @click="handleShake(scope.row)"
              :disabled="scope.row.isDisabledForShake"
              v-hasPermi="['apisecurity:applicationManage:switch']"
              >{{ scope.row.interfaceSwitch === 0 ? "下线" : "上线" }}
            </el-button>
            <el-button
              type="text"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['apisecurity:applicationManage:edit']"
              >编辑
            </el-button>
            <el-button
              type="text"
              @click="handleDelete(scope.row)"
              v-hasPermi="['apisecurity:applicationManage:remove']"
              >删除
            </el-button>
          </template>
        </template>
      </el-table-column>
    </common-table>

    <pagination
      slot="pagination"
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <Add
      ref="applicationAdd"
      @refresh="getList"
      :businessList="businessList"
    ></Add>
    <Detail ref="applicationDetail" :businessList="businessList"></Detail>
  </common-list>
</template>

<script>
import BaseList from "@/mixins/BaseList";
import Detail from "./components/detail";
import Add from "./components/add";
import {
  list,
  remove,
  updateSwitch,
  getBusinessSystemName,
} from "@/api/system/application/application";

export default {
  components: {
    Detail,
    Add,
  },
  name: "index",
  mixins: [BaseList],
  data() {
    return {
      total: 0,
      list: [],
      loading: false,
      showSearch: true,
      buttonLoading: false,
      ids: [],
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        applicationName: null,
        ip: null,
        port: null,
        domain: null,
        businessSystemId: null,
        interfaceSwitch: null,
      },
      businessList: [],
    };
  },
  methods: {
    // 批量上线下线
    changeAllStatus(status) {
      updateSwitch({
        ids: this.ids.join(","),
        status,
      }).then((res) => {
        if (res && res.code === 200) {
          this.$GlobalSuccessMessage("操作成功");
          this.getList();
        } else {
          this.$GlobalErrorConfirm("操作失败", res.msg);
        }
      });
    },
    changeStatus(row) {
      return new Promise((resolve, reject) => {
        updateSwitch({
          ids: row.id,
          status: row.interfaceSwitch === 1 ? 0 : 1,
        })
          .then((res) => {
            if (res && res.code === 200) {
              this.$GlobalSuccessMessage("操作成功");
              this.getList();
              resolve(res);
            } else {
              reject();
              this.$GlobalErrorConfirm("操作失败", res.msg);
            }
          })
          .catch(() => reject());
      });
    },

    /**
     * 获取列表数据
     */
    getList() {
      this.loading = true;
      list(this.queryParams)
        .then((res) => {
          if (res && res.code === 200) {
            this.list = res.rows.reduce((r, c) => {
              r.push({
                ...c,
                isDisabledForShake: false,
              });
              return r;
            }, []);
            this.total = res && res.total ? res.total : 0;
          } else {
            this.list = [];
            this.total = 0;
            this.handleListTips(res);
          }
        })
        .finally(() => {
          this.loading = false;
        });
    },

    //查询
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    //重置
    resetQuery() {
      this.queryParams = this.$options.data().queryParams;
    },
    // //多选
    // handleSelectionChange(selection) {
    //   this.ids = selection.map((item) => item.id);
    //   this.single = selection.length !== 1;
    //   this.multiple = !selection.length;
    // },
    //新增
    handleAdd() {
      this.$refs.applicationAdd.showDialog();
    },

    //详情
    hanldeDetail(row) {
      this.$refs.applicationDetail.showDetail(row.id);
    },
    //编辑
    handleUpdate(row) {
      if (row.interfaceSwitch === 0) {
        return this.$GlobalErrorConfirm("编辑失败", "上线状态下不允许编辑");
      }
      this.$refs.applicationAdd.showDialog(row.id);
    },
    //列表删除
    handleDelete(row) {
      if (row && row.interfaceSwitch === 0) {
        this.$GlobalErrorConfirm("警告", "上线状态不允许删除");
        return;
      }
      let interfaceSwitch = this.list.find(
        (v) => this.ids.includes(v.id) && v.interfaceSwitch === 0
      );
      if (interfaceSwitch) {
        this.$GlobalErrorConfirm("删除失败", "上线状态不允许删除");
        return;
      }
      const ids = row?.id || this.ids.join(",");
      this.$GlobalDeleteConfirm()
        .then(() => {
          remove({ ids }).then((res) => {
            if (res && res.code == 200) {
              this.$GlobalSuccessMessage("删除成功");
              if (
                this.queryParams.pageNum > 1 &&
                ids.split(",").length === this.list.length
              ) {
                this.queryParams.pageNum -= 1;
              }
              this.getList();
            } else {
              this.$GlobalErrorConfirm("删除失败", res.msg).then();
            }
          });
        })
        .catch(() => {});
    },

    getBusinessSystemName() {
      getBusinessSystemName().then((res) => {
        if (res && res.code === 200) {
          this.businessList = res.data;
        } else {
          this.businessList = [];
        }
      });
    },
  },

  created() {
    this.getList();
    this.getBusinessSystemName();
  },
};
</script>
<style lang="scss" scoped>
.ipList {
  display: flex;
  align-items: center;
  padding-right: 70px;

  .deleteIp {
    position: absolute;
    right: 0;
  }
}

.server-list {
  margin-bottom: 0 !important;

  ::v-deep .el-form-item__content {
    margin-left: 0 !important;
    display: flex;
  }

  .add-btn {
    ::v-deep .el-form-item__content {
      margin-left: 8px !important;
    }
  }
}

.port {
  ::v-deep .common-input-number {
    width: 100%;
  }
}
</style>
